﻿using DotnetSpider;
using DotnetSpider.Common;
using DotnetSpider.DataFlow;
using DotnetSpider.DataFlow.Parser;
using DotnetSpider.DataFlow.Storage;
using DotnetSpider.EventBus;
using DotnetSpider.Scheduler.Component;
using DotnetSpider.Statistics;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using OpenQA.Selenium;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace CrawlerSpiderConsole
{
    public class GithubSpider : Spider
    {
        public GithubSpider(IEventBus eventBus, IStatisticsService statisticsService,
            SpiderOptions options, ILogger<Spider> logger, IServiceProvider services)
            : base(eventBus,statisticsService,options,logger,services)
        {
            
        }


        protected override void Initialize()
        {
            // 添加自定义解析
            AddDataFlow(new Parser());
            // 使用控制台存储器
            AddDataFlow(new ConsoleStorage());

            // 添加采集请求
            AddRequests("https://github.com/zlzforever");
        }
          
    }

    public class Parser : DataParser 
    {
        protected override Task<DataFlowResult> Parse(DataFlowContext context)
        {
            var selectable = context.GetSelectable();
            // 解析数据
            var author = selectable.XPath("//span[@class='p-name vcard-fullname d-block overflow-hidden']")
                ?.GetValue();
            var name = selectable.XPath("//span[@class='p-nickname vcard-username d-block']")
                ?.GetValue();
            context.Add("author", author);
            context.Add("username", name);
  
            return new Task<DataFlowResult>(() => DataFlowResult.Success);
             
        }
    }
}
